﻿/****************************** Module Header ******************************\
 * Module Name:  OSEnvironment.cs
 * Module Version:  1.0.0 (2013-1-24)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Management;

namespace UCL
{
    /// <summary>
    /// 系统环境功能类
    /// </summary>
    public static class OSEnvironment
    {
        // 开启或关闭win7 64位系统关键目录重定向 (2013-1-25)
        [DllImport("Kernel32.dll")]
        private static extern bool Wow64EnableWow64FsRedirection(bool Wow64FsEnableRedirection);
        /// <summary>
        /// 获取System32目录路径
        /// </summary>
        /// <returns></returns>
        public static string GetSystem32Dir()
        {
            Wow64EnableWow64FsRedirection(false);
            return string.Format("{0}\\System32\\", Environment.GetFolderPath(Environment.SpecialFolder.Windows));
        }
        /// <summary>
        /// 获取Host文件
        /// <para>2013-1-24</para>
        /// </summary>
        /// <returns></returns>
        public static string GetHostFile()
        {
            Wow64EnableWow64FsRedirection(false);
            return string.Format("{0}\\System32\\drivers\\etc\\hosts", Environment.GetFolderPath(Environment.SpecialFolder.Windows));
        }

        /// <summary>
        /// 打开Inetmgr
        /// <para>2013-1-24</para>
        /// </summary>
        public static bool InvokeInetMgr()
        {
            Wow64EnableWow64FsRedirection(false);
            string path = string.Format("{0}\\System32\\inetsrv\\InetMgr.exe", Environment.GetFolderPath(Environment.SpecialFolder.Windows));
            if (File.Exists(path))
            {
                Process.Start(path);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 运行Cmd命令
        /// <para>2013-1-23</para>
        /// </summary>
        /// <param name="cmds"></param>
        /// <returns></returns>
        public static string ExecCmd(params string[] cmds)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.AutoFlush = true;
            foreach (string s in cmds)
            {
                p.StandardInput.WriteLine(s);
            }
            p.StandardInput.WriteLine("exit");
            string strRst = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            p.Close();
            return strRst;
        }

        public static string GetMachineVar(string name)
        {
            return Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Machine);
        }
        public static void SetMachineVar(string name, string value)
        {
            Environment.SetEnvironmentVariable(name, value, EnvironmentVariableTarget.Machine);
        }

        /// <summary>
        /// 获取硬盘序列号
        /// </summary>
        /// <returns></returns>
        public static string GetHardDiskID()
        {
            try
            {
                String HDid = "";
                StringBuilder sb = new StringBuilder();
                ManagementClass mc = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    foreach (PropertyData pd in mo.Properties)
                    {
                        sb.AppendFormat("{0} = {1}{2}", pd.Name, pd.Value, Environment.NewLine);
                    }
                    HDid = (string)mo.Properties["Model"].Value;
                }
                moc = null;
                mc = null;
                return HDid+Environment.NewLine+sb.ToString();
            }
            catch
            {
                return "unknow";
            }
        }

        public static string[] WMIGetMACString()
        {
            //获取网卡Mac地址   
            string mac = "";
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if ((bool)mo["IPEnabled"] == true)
                {
                    mac += mo["MacAddress"].ToString() + "|";
                }
            }
            moc = null;
            mc = null;
            return mac.Split('|');
        }
    }
}
